package cn.iocoder.yudao.module.erp.dal.mysql.purchase;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.out.ErpPurchaseOutPageReqVO;
import cn.iocoder.yudao.module.erp.controller.app.purchase.vo.in.AppErpPurchaseInPageReqVO;
import cn.iocoder.yudao.module.erp.controller.app.purchase.vo.out.AppErpPurchaseOutPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInItemDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseOutDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseOutItemsDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.ErpStockOutDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * ERP 基础出库 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ErpPurchaseOutMapper extends BaseMapperX<ErpPurchaseOutDO> {

    default PageResult<ErpPurchaseOutDO> selectPage(ErpPurchaseOutPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErpPurchaseOutDO>()
                .eqIfPresent(ErpPurchaseOutDO::getNo, reqVO.getNo())
                .eqIfPresent(ErpPurchaseOutDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ErpPurchaseOutDO::getSupplierId, reqVO.getSupplierId())
                .betweenIfPresent(ErpPurchaseOutDO::getInTime, reqVO.getInTime())
                .eqIfPresent(ErpPurchaseOutDO::getOrderNo, reqVO.getOrderNo())
                .eqIfPresent(ErpPurchaseOutDO::getRemark, reqVO.getRemark())
                .orderByDesc(ErpPurchaseOutDO::getId));
    }

    default PageResult<ErpPurchaseOutDO> selectJoinPage(ErpPurchaseOutPageReqVO reqVO) {
        MPJLambdaWrapperX<ErpPurchaseOutDO> query = new MPJLambdaWrapperX<ErpPurchaseOutDO>()
                .eqIfPresent(ErpPurchaseOutDO::getNo, reqVO.getNo())
                .eqIfPresent(ErpPurchaseOutDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ErpPurchaseOutDO::getSupplierId, reqVO.getSupplierId())
                .betweenIfPresent(ErpPurchaseOutDO::getInTime, reqVO.getInTime())
                .eqIfPresent(ErpPurchaseOutDO::getOrderNo, reqVO.getOrderNo())
                .eqIfPresent(ErpPurchaseOutDO::getRemark, reqVO.getRemark())
                .orderByDesc(ErpPurchaseInDO::getId);
        if (reqVO.getWarehouseId() != null || reqVO.getProductId() != null) {
            query.leftJoin(ErpPurchaseInItemDO.class, ErpPurchaseInItemDO::getInId, ErpPurchaseInDO::getId)
                    .eq(reqVO.getWarehouseId() != null, ErpPurchaseInItemDO::getWarehouseId, reqVO.getWarehouseId())
                    .eq(reqVO.getProductId() != null, ErpPurchaseInItemDO::getProductId, reqVO.getProductId());

            query.groupBy(ErpPurchaseInDO::getId); // 避免 1 对多查询，产生相同的 1
        }

        return selectJoinPage(reqVO, ErpPurchaseOutDO.class, query);
    }


    default PageResult<ErpPurchaseOutDO> selectAppJoinPage(AppErpPurchaseOutPageReqVO reqVO) {
        MPJLambdaWrapperX<ErpPurchaseOutDO> query = new MPJLambdaWrapperX<ErpPurchaseOutDO>()
                .eqIfPresent(ErpPurchaseOutDO::getNo, reqVO.getNo())
                .eqIfPresent(ErpPurchaseOutDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ErpPurchaseOutDO::getSupplierId, reqVO.getSupplierId())
                .betweenIfPresent(ErpPurchaseOutDO::getInTime, reqVO.getInTime())
                .eqIfPresent(ErpPurchaseOutDO::getOrderNo, reqVO.getOrderNo())
                .eqIfPresent(ErpPurchaseOutDO::getRemark, reqVO.getRemark())
                .orderByDesc(ErpPurchaseInDO::getId);
        if (reqVO.getWarehouseId() != null || reqVO.getProductId() != null || reqVO.getProductName() != null) {
            query.leftJoin(ErpPurchaseOutItemsDO.class, ErpPurchaseOutItemsDO::getOutId, ErpPurchaseInDO::getId)
                    .eq(reqVO.getWarehouseId() != null, ErpPurchaseOutItemsDO::getWarehouseId, reqVO.getWarehouseId())
                    .eq(reqVO.getProductId() != null, ErpPurchaseOutItemsDO::getProductId, reqVO.getProductId());

            if (reqVO.getProductName() != null) {
                query.leftJoin(ErpProductDO.class, ErpProductDO::getId, ErpPurchaseOutItemsDO::getProductId)
                        .likeIfPresent(ErpProductDO::getName, reqVO.getProductName());
            }

            query.groupBy(ErpPurchaseInDO::getId); // 避免 1 对多查询，产生相同的 1
        }

        if(reqVO.getOrderByType() != null){
            if(reqVO.getOrderByType() == 1){
                query.orderByDesc(ErpStockOutDO::getOutTime);
            }else if(reqVO.getOrderByType() == 2){
                query.orderByAsc(ErpStockOutDO::getOutTime);
            }
        }else {
            query.orderByDesc(ErpStockOutDO::getCreateTime);
        }

        return selectJoinPage(reqVO, ErpPurchaseOutDO.class, query);
    }



    default ErpPurchaseOutDO selectByNo(String no) {
        return selectOne(ErpPurchaseOutDO::getNo, no);
    }

    default List<ErpPurchaseOutDO> selectListByOrderId(Long orderId) {
        return selectList(ErpPurchaseOutDO::getOrderId, orderId);
    }

    default int updateByIdAndStatus(Long id, Integer status, ErpPurchaseOutDO updateObj) {
        return update(updateObj, new LambdaUpdateWrapper<ErpPurchaseOutDO>()
                .eq(ErpPurchaseOutDO::getId, id).eq(ErpPurchaseOutDO::getStatus, status));
    }


}
